#!/usr/bin/python
#coding=utf-8

#python2.7
#
#笔趣阁网站小说章节批量下载
#
#使用方法：
#将笔趣阁网站内某部小说的章节列表页面的地址赋值给xiaoshuo_url变量，
#修改file_path小说文档保存路径
#然后执行脚本即可。
#笔趣阁网站网址：https://www.biquke.com/
#
#以《全职法师》为例，网址：https://www.biquke.com/bq/37/37868/
#小说文件保存到当前目录
#
#xuwq
#2018-12-24

import sys
import re
import requests
from lxml import etree

reload(sys)  
sys.setdefaultencoding('utf8')  

#获取章节内容
def get_content(url_path, content_url):
	url = url_path + content_url;
	response = requests.get(url);
	response.encoding='utf8';
	dom_tree = etree.HTML(response.text);
	content = dom_tree.xpath("//*[@id='content']/text()");
	return content;
	
#获取url路径
def get_url_path(url):
	index = url.rfind("/") + 1;
	return url[0:index];

#小说章节列表页地址
xiaoshuo_url = "https://www.biquke.com/bq/37/37868/";
#小说内容保存路径
file_path = "./";

content = dict();
url_path = get_url_path(xiaoshuo_url);

#获取目录页内容
response = requests.get(xiaoshuo_url);
response.encoding='utf8';

dom_tree = etree.HTML(response.text);
#提取小说名称
xiaoshuo_name = dom_tree.xpath("//*[@id='info']/h1/text()")[0];
file_name = file_path+xiaoshuo_name+'.txt';

#从目录中获取小说所有章节页面的a标签
chapter_list = dom_tree.xpath("//*[@id='list']/dl/dd[*]/a");

f = open(file_name, 'w');

for dir_link in chapter_list:
	biaoti = dir_link.xpath('text()')[0];
	f.write(biaoti.encode('utf-8'));
	f.write('\n');
	
	content_url = dir_link.xpath('@href')[0];
	print biaoti;
	content_list = get_content(url_path, content_url);
	
	for content in content_list:
		f.write(content.encode('utf-8'));
	f.write('\n');
	
f.close();